package distributedbank.server.messages;

import java.io.IOException;

import distributedbank.Wrapper;
import distributedbank.server.BankBranch;

public class SyncRequest
        extends Request {

    private final Transaction transaction;
    private final int fromPort;

    public SyncRequest(int destBranchId, int fromPort, Transaction transaction) {
        super(destBranchId);
        this.fromPort = fromPort;
        this.transaction = transaction;
    }

    private static final long serialVersionUID = 1L;

    @Override
    public void runRequest(BankBranch bankBranch, Wrapper wrapper)
            throws IOException, ClassNotFoundException {
        bankBranch.syncHistory(this.transaction);

        // send the response to primary after sync is complete
        SyncRequestAck ack = new SyncRequestAck(this.transaction.transactionId);
        ack.fromPort = wrapper.localPort;
        wrapper.sendAcknowledgment(ack, this.fromPort);
    }
}
